<?php
class PlanoController extends Zend_Controller_Action
{
	protected $plano;
	
	public function ini()
	{
	}
	
	public function indexAction()
	{
		$plano = new Plano();
		$planos = $plano->fetchAll();
				
		//criando a paginaçao
		$paginator = Zend_Paginator::factory($planos);
		//pagina atual. Se nao vier nenhuma pagina, mostra a primeira
		$currentPage = $this->_getParam('page', 1);
		$paginator
		->setCurrentPageNumber($currentPage)
		->setItemCountPerPage(10);
		
		//manda para a view
		$this->view->paginator = $paginator;
	}
	
	public function createAction()
	{
	    $objForm = new Foxsis_Form_Plano();
	    $this->view->FormPlano = $objForm;
	    $this->renderScript("plano/form.phtml");
	}
	
	public function editAction()
	{
		$plano = new Plano();
		$data = $plano->find($this->_request->get('id_plano'))->current()->toArray();
		
	    $objForm = new Foxsis_Form_Plano();
	    $objForm->populate($data);
	    $this->view->FormPlano = $objForm;
	    $this->renderScript("plano/form.phtml");	
	}
	
	public function saveAction()
	{
		$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->message = $this->_flashMessenger->getMessages();
		
	    $Form = new Foxsis_Form_Plano();
	    if($this->_request->isPost())
	    {
	        $data = $this->_request->getPost();
	        if($Form->isValid($data)) 
	        {
	        	$plano = new Plano();
	        	print_r($data);
	        	if($data['id_plano'] != '')
	        	{
	            	$plano->find($data['id_plano'])->current();
	            	$plano->update($data, "id_plano = " . $data['id_plano']);
	        	}
	        	else
	        	{
	        		$data['id_usuario'] = Zend_Auth::getInstance()->getIdentity()->id_usuario;
	        		unset($data['id_plano']);
	        		$plano->insert($data);	
	        	}
	            
	            $this->_helper->FlashMessenger('Dados salvos com sucesso!');
	            return $this->_helper->redirector->goToRoute( array('controller' => 'plano'), null, true);   
	        }
	        else 
	        {
	            $this->_helper->FlashMessenger('Erro ao cadastrar plano!');
	            $Form->populate($data);
	        } 
	        
	        $this->view->FormPlano = $Form;
	        $this->_forward('create',null,null,$data);
	    }	
	}
	
	public function deleteAction()
	{
		try
		{
			if($this->_request->get('id_plano'))
			{
				$id_plano = $this->_request->get('id_plano');
				$dominio = new Dominio();
				$data = $dominio->fetchAll($dominio->select()->where('id_plano = '. $id_plano));
				
				if(!count($data))
				{
					$plano = new Plano();
					$plano->find($id_plano)->current();
					$plano->delete('id_plano = ' . $id_plano);

					$this->_helper->FlashMessenger('Registro deletado com sucesso!');
					return $this->_helper->redirector->goToRoute( array('controller' => 'plano'), null, true);
				}
				else
				{
					$this->_helper->FlashMessenger('Existem registros amarrados ao plano selecionado!');	
					return $this->_helper->redirector->goToRoute( array('controller' => 'plano'), null, true);
				}
			}	
		} 
		catch (Exception $e)
		{
			$this->_helper->FlashMessenger('Erro ao deletar o registro!');
			return $this->_helper->redirector->goToRoute( array('controller' => 'plano'), null, true);	
		}
	}
}